#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Create by zhang
# Create on 2022/7/27 8:32
import wx

from infrastructure.constant.response_constant import ResponseConstant
from infrastructure.rules.validators.validator_interface import ValidatorInterface, ValidatorModeEnum


class Validator(wx.Validator):
    def __init__(self, validator:ValidatorInterface):
        super(Validator, self).__init__()
        self.validator:ValidatorInterface = validator
        # self.Bind(wx.EVT_CHAR, self.OnCharChanged)  # 绑定字符输入事件

    def OnCharChanged(self, event):
        # 得到输入字符的 ASCII 码
        keycode = event.GetKeyCode()
        # 退格（ASCII 码 为8），删除一个字符。
        if keycode == 8:
            # self.StringLength -= 1
            # 事件继续传递
            event.Skip()
            return

        # 把 ASII 码 转成字符
        InputChar = chr(keycode)

        result = self.validator.validate(InputChar, ValidatorModeEnum.ATOM)
        return True if result.code == ResponseConstant.SUCCESS else False

    def Clone(self):
        return Validator(validator=self.validator)

    def Validate(self, win):
        textCtrl = self.GetWindow()
        text = textCtrl.GetValue()
        return self.validator.validate(text, ValidatorModeEnum.ENTITY)

    def TransferToWindow(self):
        return True

    def TransferFromWindow(self):
        return True
